package org.sction.action.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.sction.action.GridAction;
import org.sction.action.annotation.Action;
import org.sction.action.annotation.Controller;
import org.sction.action.annotation.Parameter;
import org.sction.bean.FileBean;
import org.sction.task.DBackUpTask;
import org.sction.util.FileUtils;
import org.sction.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;


@Controller("system/db")
public class DbAction extends GridAction {
	@Parameter
	public String path;
	@Parameter
	public String name;
	@Autowired
	public DataSource dataSource;
	public static String DB_BACKUP_DIR;
	public static String DB_Name;

	@Action
	public Map<String, Object> search() {
		File backdir = new File(DB_BACKUP_DIR);
		File[] files = backdir.listFiles();
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (this.sord.equalsIgnoreCase("desc")) {
			for (int i = files.length - 1; i >= 0; i--) {
				File file = files[i];
				Map<String, String> map = new HashMap<String, String>();
				map.put("path", file.getPath());
				map.put("name", file.getName());
				map.put("time", sdf.format(new Date(file.lastModified())));
				map.put("size", FileUtils.getFileSize(file.length()));
				data.add(map);
			}
		} else {
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				Map<String, String> map = new HashMap<String, String>();
				map.put("path", file.getPath());
				map.put("name", file.getName());
				map.put("time", sdf.format(new Date(file.lastModified())));
				map.put("size", FileUtils.getFileSize(file.length()));
				data.add(map);
			}
		}
		Map<String, Object> mapx = new HashMap<String, Object>();
		mapx.put("rows", data);
		return mapx;
	}

	@Action
	public Map<String, Object> backup() {
		DBackUpTask db = new DBackUpTask();
		db.setBackupDir(DB_BACKUP_DIR);
		db.setDataBaseName(DB_Name);
		db.setDataSource(dataSource);
		db.setNameprefix("人工备份");
		db.backupMsSql();
		if ("".equals(db.getError())) {
			return this.pushState(db.getPath(), 1);
		} else {
			return this.pushState(db.getError(), 0);
		}
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Action
	public Map<String, Object> restore() {
		DBackUpTask db = new DBackUpTask();
		db.setDataBaseName(DB_Name);
		db.setDataSource(dataSource);
		db.setBackupDir(DB_BACKUP_DIR);
		db.setNameprefix("恢复[" + name + "]前系统自动备份--");
		db.backupMsSql();
		if ("".equals(db.getError())) {
			db.setPath(DB_BACKUP_DIR + "/" + name);

			db.restoreMsSql();
			if ("".equals(db.getError())) {
				return this.pushState(db.getPath(), 1);
			} else {
				return this.pushState(db.getError(), 0);
			}
		} else {
			return this.pushState("恢复数据前系统自动备份--" + db.getError(), 0);
		}
	}

	@Action
	public Map<String, Object> remove() {
		try {
			File file = new File(DB_BACKUP_DIR + "/" + name);
			System.out.println(file.getPath());
			System.out.println(file.exists());
			if (file.delete()) {
				return this.pushState("", 1);
			} else {
				return this.pushState("", 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return this.pushState(e.getMessage(), 0);
		}
	}

	@Action
	public FileBean download() {
		try {
			name = new String(name.getBytes(StringUtils.getEncoding(name)));
		} catch (UnsupportedEncodingException e1) {
		}
		FileBean fb = new FileBean(DB_BACKUP_DIR + "/" + name);
		return fb;
	}

	public void setDB_BACKUP_DIR(String dB_BACKUP_DIR) {
		DB_BACKUP_DIR = dB_BACKUP_DIR;
	}

	public void setDB_Name(String dB_Name) {
		DB_Name = dB_Name;
	}

}
